package jsp;

import entity.Emp;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("*.session")
public class SessionServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getServletPath();
        switch (path){
            case "/add.session":
                addSession(req,resp);
                break;
            case "/find.session":
                findSession(req,resp);
                break;
            case "/modify.session":
                modifySession(req,resp);
                break;
            case "/remove.session":
                removeSession(req,resp);
                break;
            case "/clean.session":
                cleanSession(req,resp);
                break;
        }
    }

    protected void addSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取session对象
        HttpSession session = req.getSession();
        session.setAttribute("name","管理员");
        // 注意点:在session中,提供了生存时间的用法
        // 但是,该生存时间针对的是整个session对象
        // 在正式开发中,不建议修改session生存时间
        // 默认生存时间为半小时
        // 当生存时间设置为0或者负数的时候,表示永不过期
        session.setMaxInactiveInterval(0);
        Emp emp = new Emp();
        emp.setId(1);
        emp.setName("admin");
        emp.setSalary(8000.0);
        session.setAttribute("emp",emp);
        User user = new User(1,"admin","123456","13812345678","江苏-南京");
        session.setAttribute("user",user);
        resp.sendRedirect(req.getContextPath()+"/jsp/session.jsp");
    }

    protected void findSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String name = (String) req.getSession().getAttribute("name");
        Emp emp = (Emp) session.getAttribute("emp");
        User user = (User) session.getAttribute("user");
        System.out.println(name);
        System.out.println(emp);
        System.out.println(user);
        resp.sendRedirect(req.getContextPath()+"/jsp/session.jsp");
    }

    protected void modifySession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 当key相同时,会覆盖
        req.getSession().setAttribute("name","张三");
        req.getRequestDispatcher("/jsp/session.jsp").forward(req,resp);
    }

    protected void removeSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 可以移除指定key的数据
        HttpSession session = req.getSession();
        session.removeAttribute("name");
        req.getRequestDispatcher("/jsp/session.jsp").forward(req,resp);
    }

    protected void cleanSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 可以清除当前session中所有的数据
        HttpSession session = req.getSession();
        // 此时不是通过方法去清除,而是通过销毁session去清除
        session.invalidate();
        req.getRequestDispatcher("/jsp/session.jsp").forward(req,resp);
    }



}
